package com.pactera.db;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import com.pactera.consts.Const;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

public class OracleDb {

	public static HikariDataSource ds = null;
	private static final QueryRunner runner = new QueryRunner();
	
	public static void init(){
//		HikariConfig config = new HikariConfig("src/main/resource/hikari.properties");
		HikariConfig config = new HikariConfig();
		config.setJdbcUrl(Const.ORACLE_URL); //数据源
        config.setUsername(Const.ORACLE_USER); //用户名
        config.setPassword(Const.ORACLE_PASSWD); //密码
        config.setDriverClassName("oracle.jdbc.driver.OracleDriver");
        config.addDataSourceProperty("cachePrepStmts", "true"); //是否自定义配置，为true时下面两个参数才生效
        config.addDataSourceProperty("prepStmtCacheSize", "250"); //连接池大小默认25，官方推荐250-500
        config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); //单条语句最大长度默认256，官方推荐2048
        config.addDataSourceProperty("useServerPrepStmts", "true"); //新版本MySQL支持服务器端准备，开启能够得到显著性能提升
        config.addDataSourceProperty("useLocalSessionState", "true");
        config.addDataSourceProperty("useLocalTransactionState", "true");
        config.addDataSourceProperty("rewriteBatchedStatements", "true");
        config.addDataSourceProperty("cacheResultSetMetadata", "true");
        config.addDataSourceProperty("cacheServerConfiguration", "true");
        config.addDataSourceProperty("elideSetAutoCommits", "true");
        config.addDataSourceProperty("maintainTimeStats", "false");

		ds = new HikariDataSource(config);	
	}
	
	 public static Connection getConnection() {
	        Connection conn = null;
	        if (null != ds) {
	            try {
	                conn =  ds.getConnection();
	            } catch (SQLException e) {
	                e.printStackTrace();
	            }
	        }
	        return conn;
	    }

	    // 关闭数据库连接
	    public static void closeConn(Connection conn) {
	        try {
	            if (conn != null) {
	                conn.close();
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	    }
	
	    public static <T> T query(String sql, ResultSetHandler<T> rsh, Object... params) {
	        Connection conn = getConnection();
	        T t = null;
	        try {
	             t = runner.query(conn ,sql, rsh, params);
	        } catch (SQLException e) {
	            e.printStackTrace();
	        }finally {
	            closeConn(conn);
	        }
	        return t;
	    }

	    public static <T> T query(String sql, ResultSetHandler<T> rsh)  {
	        Connection conn = getConnection();
	        T t = null;
	        try {
	            t = runner.query(conn ,sql, rsh);
	        } catch (SQLException e) {
	            e.printStackTrace();
	        }finally {
	            closeConn(conn);
	        }
	        return t;
	    }

	    public static int update(String sql, Object... params)  {
	        Connection conn = getConnection();
	        int t = 0;
	        try {
	            t = runner.update(conn ,sql, params);
	        } catch (SQLException e) {
	            e.printStackTrace();
	        }finally {
	            closeConn(conn);
	        }
	        return t;
	    }

	    public static int update(String sql)  {
	        Connection conn = getConnection();
	        int t = 0;
	        try {
	            t = runner.update(conn ,sql);
	        } catch (SQLException e) {
	            e.printStackTrace();
	        }finally {
	            closeConn(conn);
	        }
	        return t;
	    }

}
